package com.lvchuang.controller;

import com.lvchuang.model.News;
import com.lvchuang.model.NewsCategory;
import com.lvchuang.service.NewsCategoryService;
import com.lvchuang.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zgr
 * @Date: 2019/9/23 11:00
 * @Description:
 */
@Controller
@RequestMapping("/newsController")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsCategoryService newsCaService;

    @RequestMapping("/newsSelect")
    public String selectAll(Map map) {
//        查询新闻表的数据
        List<News> news = newsService.selectAll();
        map.put("news", news);
        for (News news2 : news) {
//            System.out.println(news2);
        }
        return "userlist";
    }

    //    跳转到增加页面
    @RequestMapping("/insertNewsList")
    public String insertNewsList() {
        return "addadmin";
    }

    //    增加功能
    @RequestMapping("/insertNews")
    @ResponseBody
    public String insertNews(News news) {
        System.out.println(news);
        try {
            //新增账号逻辑
            newsService.insertNews(news);
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
            return "error";
        }
        return "success";
    }

    //    删除功能
    @RequestMapping("/deleteNews")
    public String deleteNews(@RequestParam("id") int id, Map map) {
        newsService.deleteNews(id);
        List<News> news = newsService.selectAll();
        map.put("news", news);
        for (News news2 : news) {
//            System.out.println(news2);
        }
        return "userlist";
    }

    @RequestMapping("/updateNewsList")
    public String updateNewsList(@RequestParam("id") int id, Map map) {
        News news = newsService.selectById(id);
        System.out.println(news);
        int category_id = news.getCategory_id();
//		System.out.println(category_id);
        NewsCategory newcate = newsCaService.SelectByCategoryId(category_id);
        map.put("newcate", newcate);
        map.put("news", news);
        return "updateadmin";
    }

    @RequestMapping("/updateNews")
    @ResponseBody
    public String updateNews(News news, @RequestParam("category_name") String category_name) {
        System.out.println(category_name);
        System.out.println(news);
        try {
            //修改逻辑
            NewsCategory newcate = newsCaService.selectByName(category_name);
//            System.out.println(newcate);
            news.setCategory_id(newcate.getCategory_id());
            newsService.updateNewsByNews(news);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return "error";
        }
        return "success";
    }
}
